package com.ss.android.article.browser.view;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;

import com.ss.android.article.browser.FeedFragment;
import com.ss.android.article.browser.WebViewClientDelegate;
import com.ss.android.article.browser.c.SystemShareHelper;
import com.ss.android.common.dialog.AlertDialog;

import java.util.Map;

public final class BrowserWebViewClient extends WebViewClient {
    boolean isPageLoading;
    @NonNull
    private final Activity mActivity;
    @NonNull
    private final Browser mBrowser;
    @NonNull
    private final WebViewClientDelegate mWebViewClientDelegate;
    private volatile boolean e;
    private float f;

    BrowserWebViewClient(@NonNull Activity activity, @NonNull FeedFragment feedFragment, @NonNull Browser browser) {
        this.e = false;
        this.f = 0f;
        this.isPageLoading = false;
        this.mActivity = activity;
        this.mBrowser = browser;
        this.mWebViewClientDelegate = feedFragment;
        com.ss.android.article.browser.c.f.a();
        new SystemShareHelper(activity);
    }

    static float a(BrowserWebViewClient arg0, float arg1) {
        arg0.f = arg1;
        return arg1;
    }

    static boolean a(BrowserWebViewClient arg1) {
        arg1.e = false;
        return false;
    }

    public final void onFormResubmission(WebView arg5, @NonNull Message arg6, @NonNull Message arg7) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.mActivity);
        builder.setTitle(this.mActivity.getString(com.ss.android.article.browser.R.string.a02));
        builder.setMessage(this.mActivity.getString(com.ss.android.article.browser.R.string.q4)).setCancelable(true).setPositiveButton(this.mActivity.getString(com.ss.android.article.browser.R.string.cp), new q(arg7)).setNegativeButton(this.mActivity.getString(com.ss.android.article.browser.R.string.c9), new p(arg6));
        AlertDialog dialog = builder.create();
        dialog.show();
        android.support.design.a.resizeDialog(this.mActivity, ((Dialog) dialog));
    }

    @TargetApi(value = 19)
    public final void onPageFinished(@NonNull WebView webView, String url) {
        this.isPageLoading = false;
        if (webView.isShown()) {
            this.mWebViewClientDelegate.setCanGoBack(webView.canGoBack());
            this.mWebViewClientDelegate.setCanGoForward(webView.canGoForward());
            webView.postInvalidate();
        }

        if (webView.getTitle() == null || (webView.getTitle().isEmpty())) {
            this.mBrowser.a.setTitle(this.mActivity.getString(com.ss.android.article.browser.R.string.a1u));
        } else {
            this.mBrowser.a.setTitle(webView.getTitle());
        }

        if (Build.VERSION.SDK_INT >= 19 && (this.mBrowser.isHardwareAccelerateEnabled)) {
            webView.evaluateJavascript("javascript:(function(){var e=\'img {-webkit-filter: invert(100%);\'+\'-moz-filter: invert(100%);\'+\'-o-filter: invert(100%);\'+\'-ms-filter: invert(100%); }\',t=document.getElementsByTagName(\'head\')[0],n=document.createElement(\'style\');if(!window.counter){window.counter=1}else{window.counter++;if(window.counter%2==0){var e=\'html {-webkit-filter: invert(0%); -moz-filter: invert(0%); -o-filter: invert(0%); -ms-filter: invert(0%); }\'}}n.type=\'text/css\';if(n.styleSheet){n.styleSheet.cssText=e}else{n.appendChild(document.createTextNode(e))}t.appendChild(n)})();", null);
        }

        this.mWebViewClientDelegate.onPageFinished();
    }

    public final void onPageStarted(WebView webView, String url, Bitmap favicon) {
        this.isPageLoading = true;
        r.a(null);
        this.mBrowser.isWebViewVisible();
        this.mWebViewClientDelegate.onPageStarted();
    }

    public final void onReceivedHttpAuthRequest(WebView arg12, @NonNull HttpAuthHandler arg13, String arg14, String arg15) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.mActivity);
        View v4 = LayoutInflater.from(this.mActivity).inflate(com.ss.android.article.browser.R.layout.cf, null);
        View v0 = v4.findViewById(com.ss.android.article.browser.R.id.qz);
        View v1 = v4.findViewById(com.ss.android.article.browser.R.id.r0);
        View v2 = v4.findViewById(com.ss.android.article.browser.R.id.r1);
        ((TextView) v0).setText(this.mActivity.getString(com.ss.android.article.browser.R.string.o5, new Object[]{arg15}));
        builder.setCustomTitle(v4).setTitle(com.ss.android.article.browser.R.string.a0d)
                .setCancelable(true)
                .setPositiveButton(com.ss.android.article.browser.R.string.a0d, new m(((EditText) v1), ((EditText) v2), arg13)).setNegativeButton(com.ss.android.article.browser.R.string.bg, new l(arg13));
        AlertDialog dialog = builder.create();
        dialog.show();
        android.support.design.a.resizeDialog(this.mActivity, ((Dialog) dialog));
    }

    public final void onReceivedSslError(WebView arg1, @NonNull SslErrorHandler arg2, @NonNull SslError arg3) {
        arg2.proceed();
    }

    @TargetApi(value = 19)
    public final void onScaleChanged(@NonNull WebView arg5, float arg6, float arg7) {
        float v2 = 100f;
        if ((arg5.isShown()) && (this.mBrowser.mPreferenceManager.getTextReflow()) && Build.VERSION.SDK_INT >= 19 && !this.e && Math.abs(v2 - v2 / this.f * arg7) > 2.5f && !this.e) {
            this.e = arg5.postDelayed(new n(this, arg7, arg5), 100);
        }
    }

    @TargetApi(value = 21)
    public final WebResourceResponse shouldInterceptRequest(WebView webView, @NonNull WebResourceRequest webResourceRequest) {
        return super.shouldInterceptRequest(webView, webResourceRequest);
    }

    @TargetApi(value = 20)
    @Nullable
    public final WebResourceResponse shouldInterceptRequest(WebView arg2, String arg3) {
        return null;
    }

    public final boolean shouldOverrideUrlLoading(@NonNull WebView arg6, @NonNull String arg7) {
        int v2;
        boolean v0 = false;
        Map v3 = this.mBrowser.mAdditionalHttpHeaders;
        if (!this.mBrowser.b) {
            URLUtil.isAboutUrl(arg7);
        }

        if (!v3.isEmpty()) {
            v2 = Build.VERSION.SDK_INT >= 19 ? 1 : 0;
            if (v2 == 0) {
                v2 = 0;
            } else {
                arg6.loadUrl(arg7, v3);
                v2 = 1;
            }
        } else {
            v2 = 0;
        }

        if (v2 != 0 || (super.shouldOverrideUrlLoading(arg6, arg7))) {
            v0 = true;
        }

        return v0;
    }
}

